package pww.service.serviceImpl;

import a.annotation.Component;
import a.annotation.Inject;
import dbUtil.SQLExecteException;
import dbUtil.SQLExecute;
import pww.dao.StudentDao;
import pww.entity.Student;
import pww.service.StudentService;
import pww.utils.SQlExcuteFactory;

import java.util.List;

@Component
public class StudentServiceImpl implements StudentService {
    @Inject("studentDaoImpl")
    private StudentDao studentDaoimpl;
    public List<Student> queryAllStudent() {
        List<Student> students = studentDaoimpl.queryAllStudent();
        return students;
    }
    @Override
    public int insertStudent(Student student) {
        SQLExecute sqlExecute = SQlExcuteFactory.currentSqlExcute();
        try {
            sqlExecute.beginTranstaction();
            Student student1 = studentDaoimpl.find(student.getName(),sqlExecute);
            if (student1==null) {
                studentDaoimpl.insertStudent(student,sqlExecute);
                sqlExecute.commit();
                return 1;
            }
            sqlExecute.rollback();
            return 0;
        }catch (SQLExecteException e){
            sqlExecute.rollback();
            return 0;
        }
    }
}
